<?php

class login extends NTRSAction {

    public function get ($request) {

        $request->template = 'nitrous-cms/themes/' . NTRS_THEME . '/login.tpl.php';

    }

    public function post ($request) {

        $request->template = 'nitrous-cms/themes/' . NTRS_THEME . '/login.tpl.php';

        $model = $request->loadFormModel('login');

        if ($model->validate()) {

            $auth = new NTRSAuth($request);

            // Authenticate user
            if ($auth->authenticate($model->get('username'), $model->get('password'), (bool)$model->get('remember_me'))) {

                // Redirect the user to the refering request uri if available
                if ($request->session->get('nitrous_auth_refer_request_uri',false)) {

                    $request->redirect($request->session->get('nitrous_auth_refer_request_uri'));
                    
                } else {

                    $request->redirect('/secure_area_default');

                }
                exit;

            } else {

                $request->attribs->set('login_error', $auth->getErrorMsg());

            }

        }

    }

}